package com.mooc.fuxi.network;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 双重锁单例
 * */
public class NetRepo {

    private NetRepo(){createRetrofit();}

    private volatile static NetRepo instance;

    public static NetRepo getInstance() {
        if (instance == null){
            synchronized (NetRepo.class){
                if (instance == null){
                    instance = new NetRepo();
                }
            }
        }
        return instance;
    }

    private Retrofit retrofit;

    public Retrofit getRetrofit() {
        return retrofit;
    }

    private void createRetrofit(){
        OkHttpClient.Builder okBuilder = new OkHttpClient.Builder();
        okBuilder.addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY));
        okBuilder.addInterceptor(new HeadInterceptor());
        Retrofit.Builder builder = new Retrofit.Builder();
        builder.baseUrl("http://82.156.178.182:8087/api/");
        builder.client(okBuilder.build());
        builder.addConverterFactory(GsonConverterFactory.create());
        builder.addCallAdapterFactory(RxJava2CallAdapterFactory.create());
        retrofit = builder.build();
    }




}
